import { BasicColumn, FormSchema } from '@/components/Table';
import { h, ref } from 'vue';
import { Tag } from 'ant-design-vue';
import { ywUserSelectList } from '@/api/heng/ywManage';
import { ApiSelect } from '@/components/Form';
import util from '@/utils/util';

import { useMessage } from '@/hooks/web/useMessage';
import Ainput from '@/views/bookManage/components/account/Ainput.vue';
import { VxeFormItemProps, VxeGridPropTypes } from '@/components/VxeTable';
import { changeEnd } from 'codemirror';

type CheckedType = boolean | string | number;
// BasicColumn[]

// VxeColumnPropTypes.CellRender

/**
 */
export const vxeTableColumns: VxeGridPropTypes.Columns = [
  {
    type: 'checkbox',
    title: '',
    field: 'seq',
    width: 40,
  },
  {
    title: '审批编号',
    field: 'approveNo',
    showOverflow: 'title',
    filters: [{ data: '' }],
    filterRender: {
      name: 'VxeInput',
    },
    formatter: ({ cellValue, row, column }) => {
      let val = cellValue;
      if (!row.stateName) {
        return val;
      }
      val = `${val}(${row.stateName})`;
      return val;
    },

    // width: 130,
  },
  {
    title: '订单总金额人民币',
    field: 'totalAllPrice',
    align: 'center',
    showOverflow: 'title',
    width: 130,
  },
  {
    title: '抽成比例',
    field: 'orderCommissionRate',
    align: 'center',
    formatter: ({ cellValue }) => {
      return cellValue + '%';
    },
    width: 100,
  },
  {
    title: '抽成金额',
    field: 'orderCommissionPrice',
    align: 'center',
    showOverflow: 'title',
    slots: { footer: 'footer_orderCommissionPrice' },
    width: 130,
  },
  {
    title: '申请时间',
    field: 'createTime',
    showOverflow: 'title',
    align: 'center',
    width: 140,
  },
  {
    title: '当前审批人',
    field: 'currentUserName',
    align: 'center',
    width: 130,
  },
  {
    title: '发起人',
    field: 'userName',
    align: 'center',
    width: 70,
  },
  {
    title: '部门',
    field: 'deptName',
    align: 'center',
    width: 100,
  },
  {
    title: '审批状态',
    field: 'status',
    slots: { default: 'state' },
    align: 'center',
    width: 100,
  },
  {
    width: 230,
    title: '操作',
    align: 'center',
    field: '',
    slots: { default: 'action' },
    fixed: 'right',
  },
];

export const vxeTableFormSchema: VxeFormItemProps[] = [
  {
    span: 5,
    field: 'keyword',
    title: '查询',
    itemRender: {
      props: {
        placeholder: '审批编号，申请人',
      },
      name: 'AInput',
    },
  },
  // {
  //   span: 5,
  //   field: 'status',
  //   title: '审批状态',
  //   itemRender: {
  //     name: 'ASelect',
  //     defaultValue: 2,
  //     props: {
  //       style:{width: '100%'},
  //       options: [
  //         { label: '审批中', value: 1 },
  //         { label: '已通过', value: 2 },
  //         { label: '已驳回', value: 99 },
  //         { label: '已撤销', value: 0 },
  //       ],
  //     },
  //   },
  // },
  {
    span: 5,
    field: 'timeArr',
    title: '时间区间',
    itemRender: {
      props: {
        valueFormat: 'YYYY-MM-DD',
        format: 'YYYY-MM-DD',
      },
      name: 'ARangePicker',
    },
  },

  {
    span: 5,
    itemRender: {
      name: 'VxeButtonGroup',
      options: [
        { type: 'reset', content: '重置' },
        { type: 'submit', content: '查询', status: 'primary' },
      ],
    },
  },
];

export const rules = {
  mouldNameObj: [{ required: true, message: '费用名称不能为空', trigger: 'change' }],
  materialNo: [
    { required: true, message: '物料编号不能为空', trigger: 'change' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  orderNo: [
    { required: true, message: '采购单号不能为空', trigger: 'change' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
};

/**
 * 审批列表
 */

export const confirmColumns: VxeGridPropTypes.Columns = [
  {
    title: '审批编号',
    field: 'approveNo',
    showOverflow: 'title',
    // width: 130,
  },
  {
    title: '订单总金额人民币',
    field: 'totalAllPrice',
    align: 'center',
    showOverflow: 'title',
    width: 130,
  },
  {
    title: '抽成比例',
    field: 'orderCommissionRate',
    align: 'center',
    formatter: ({ cellValue }) => {
      return cellValue + '%';
    },
    width: 100,
  },
  {
    title: '抽成金额',
    field: 'orderCommissionPrice',
    slots: { footer: 'footer_orderCommissionPrice' },
    align: 'center',
    showOverflow: 'title',
    width: 130,
  },
  {
    title: '部门',
    field: 'deptName',
    align: 'center',
    width: 100,
  },
];

export function paseTags(row) {
  let val = '';
  let color = '';
  switch (row.status) {
    case 1:
      val = '审批中';
      color = 'orange';
      break;
    case 2:
      val = '已通过';
      color = 'green';

      break;
    case 99:
      val = '已驳回';
      color = 'red';

      break;
    // case 0:
    //   val = '已撤销';
    //   break;
    default:
      break;
  }
  return { color, val };
}

export function checkboxCountFn(rows) {
  let totalAllPrice = 0;
  let orderCommissionPrice = 0;
  let manageUserPrice = 0;
  for (let r1 in rows) {
    let item = rows[r1];
    if (item.totalAllPrice) {
      totalAllPrice = totalAllPrice + item.totalAllPrice;
    }
    if (item.orderCommissionPrice) {
      orderCommissionPrice = orderCommissionPrice + item.orderCommissionPrice;
    }
    if (item.manageUserPrice) {
      manageUserPrice = manageUserPrice + item.manageUserPrice;
    }
  }
  let totalObj = {
    totalAllPrice: totalAllPrice ? totalAllPrice.toFixed(2) : null,
    orderCommissionPrice: orderCommissionPrice ? orderCommissionPrice.toFixed(2) : null,
    manageUserPrice: manageUserPrice ? manageUserPrice.toFixed(2) : null,
  };
  return totalObj;
}

export const addExcludeColumns: VxeGridPropTypes.Columns = [
  {
    title: '单据来源编号',
    field: 'approveNo',
    showOverflow: 'title',
    // width: 130,
  },

  {
    title: '总金额(人民币)',
    field: 'totalAllPrice',
    showOverflow: 'title',
    slots: {
      footer: 'footer_totalAllPrice',
    },
    width: 100,
  },
  {
    title: '抽成比例',
    field: 'orderCommissionRate',
    formatter: ({ cellValue }) => {
      return cellValue + '%';
    },
    showOverflow: 'title',
    width: 100,
  },
  {
    title: '订单抽成金额',
    field: 'orderCommissionPrice',
    slots: {
      footer: 'footer_orderCommissionPrice',
    },
    showOverflow: 'title',
    width: 100,
  },

  {
    title: '发起人',
    field: 'userName',
    align: 'center',
    width: 100,
  },
  {
    title: '部门',
    field: 'deptName',
    align: 'center',
    width: 100,
  },
  {
    width: 80,
    title: '操作',
    align: 'center',
    slots: { default: 'action' },
    fixed: 'right',
  },
];
export const getExcludeColumns: VxeGridPropTypes.Columns = [
  {
    title: '单据来源编号',
    field: 'orderNo',
    showOverflow: 'title',
  },

  {
    title: '总金额(人民币)',
    field: 'totalAllPrice',
    showOverflow: 'title',
    slots: {
      footer: 'footer_totalAllPrice',
    },
    width: 100,
  },
  {
    title: '抽成比例',
    field: 'orderCommissionRate',
    formatter: ({ cellValue }) => {
      return cellValue + '%';
    },
    showOverflow: 'title',
    width: 100,
  },
  {
    title: '订单抽成金额',
    field: 'orderCommissionPrice',
    slots: {
      footer: 'footer_orderCommissionPrice',
    },
    showOverflow: 'title',
    width: 100,
  },

  {
    title: '发起人',
    field: 'userName',
    align: 'center',
    width: 100,
  },
  {
    title: '部门',
    field: 'salesmanDeptName',
    align: 'center',
    width: 100,
  },
  {
    width: 80,
    title: '操作',
    align: 'center',
    slots: { default: 'action' },
    fixed: 'right',
  },
];
